package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.base.GoodsApi;
import com.jmp.base.dto.*;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.event.UpdateGoodsAsyncEvent;
import one.stand.mapper.GoodsServiceCrmMapper;
import one.stand.model.Goods;
import one.stand.model.GoodsGroupBuying;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.GoodsGroupService;
import one.stand.service.GoodsImportService;
import one.stand.service.GoodsService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author cjr
 */
@RestController
public class GoodsFeignController implements GoodsApi {

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsImportService goodsImportService;
    @Autowired
    private GoodsServiceCrmMapper goodsServiceCrmMapper;
    @Autowired
    private GoodsGroupService goodsGroupService;

    @Override
    public ResultModel save(@RequestBody GoodsSaveRequest request) {
        Goods model = new Goods();
        model.setGoodsId(request.getGoodsId());
        model.setGoodsName(request.getGoodsName());
        model.setGoodstypeId(request.getGoodstypeId());
        model.setGoodsDetails(request.getGoodsDetails());
        model.setIsPayment(request.getIsPayment());
        model.setShowPrice(request.getShowPrice());
        model.setCoverImg(request.getCoverImg());
        model.setVideoUrl(request.getVideoUrl());
        model.setWholesaleInfo(request.getWholesaleInfo());
        model.setWholesalePrice(request.getWholesalePrice());
        model.setRetailPrice(request.getRetailPrice());
        model.setContactInfo(request.getContactInfo());
        model.setFromApp(request.getFromApp());

        Integer companyId = request.getCompanyId();
        String specIds = request.getSpecIds();
        String photos = request.getGoodPhoto();
        goodsService.addGoodsInfo(model, companyId, specIds, photos, request.getSpecList());
        return ResultModel.success();
    }

    @Override
    public ResultModel save2(@RequestBody GoodsSaveRequest request) {
        Goods model = new Goods();
        model.setGoodsId(request.getGoodsId());
        model.setGoodsName(request.getGoodsName());
        model.setGoodstypeId(request.getGoodstypeId());
        model.setGoodsDetails(request.getGoodsDetails());
        model.setIsPayment(request.getIsPayment());
        model.setShowPrice(request.getShowPrice());
        model.setCoverImg(request.getCoverImg());
        model.setVideoUrl(request.getVideoUrl());
        model.setWholesaleInfo(request.getWholesaleInfo());
        model.setContactInfo(request.getContactInfo());
        model.setFromApp(request.getFromApp());

        Integer companyId = request.getCompanyId();
        String photos = request.getGoodPhoto();

        if (OneListUtil.isNotEmpty(request.getSpecList())) {
            Set<String> strSet = request.getSpecList().stream().map(GoodsSaveRequest.Spec::getSpecName).collect(Collectors.toSet());
            AssertUtil.isTrue(strSet.size() == request.getSpecList().size(), ResultEnum.PARAM_CHECK, "SKU名称重复");
            for (GoodsSaveRequest.Spec spec : request.getSpecList()) {
                strSet = spec.getGoodSpecModelList().stream().map(GoodsSaveRequest.Spec::getSpecAttribute).collect(Collectors.toSet());
                AssertUtil.isTrue(strSet.size() == spec.getGoodSpecModelList().size(), ResultEnum.PARAM_CHECK, "SKU名称重复");
            }
        }

        goodsService.save(model, companyId, photos, request.getSpecList());
        return ResultModel.success();
    }

    @Override
    public ResultModel remove(@RequestBody GoodsRemoveDto request) {
        goodsService.delGoodsInfo(request.getGoodsId());
        return ResultModel.success();
    }

    @Override
    public ResultModel saveImport(@RequestBody GoodsSaveImportRequest request) {
        for (List<String> list : request.getLo()) {
            goodsImportService.saveGoods(request.getCompanyId(), list);
        }

        goodsServiceCrmMapper.updateBySpec(null);
        applicationContext.publishEvent(new UpdateGoodsAsyncEvent(request.getCompanyId(), null));

        return ResultModel.success();
    }

    @Override
    public ResultModel deleteImport(@RequestBody GoodsDeleteImportRequest request) {
        goodsImportService.removeGoods(request.getCompanyId());
        return ResultModel.success();
    }

    @Override
    public ResultModel platformTypeUpdate(@RequestBody GoodsPlatformTypeUpdateRequest request) {
        goodsService.updatePlatformType(request.getGoodsId(), request.getTypeIds(), request.getPopularityLevel());
        return ResultModel.success();
    }

    @Override
    public ResultModel platformTypeDelete(@RequestBody GoodsPlatformTypeDeleteRequest request) {
        goodsService.deletePlatformType(request.getGoodsId());
        return ResultModel.success();
    }

    @Override
    public ResultModel groupSave(@RequestBody GoodsGroupSaveDto request) {
        GoodsGroupBuying goodsGroupBuying = new GoodsGroupBuying();
        goodsGroupBuying.setId(request.getId());
        goodsGroupBuying.setTitle(request.getTitle());
        goodsGroupBuying.setPhoto(request.getPhoto());
        goodsGroupBuying.setHidePhotos(request.getHidePhotos());
        goodsGroupBuying.setVideoUrl(request.getVideoUrl());
        goodsGroupBuying.setGoodsId(request.getGoodsId());
        goodsGroupBuying.setEndTime(request.getEndTime());
        goodsGroupService.save(goodsGroupBuying);
        return ResultModel.success();
    }

    @Override
    public ResultModel groupUp(@RequestBody GoodsGroupUpDto request) {
        goodsGroupService.up(request.getId(), request.getLabelIds(), request.getPushTitle(), request.getPushContent());
        return ResultModel.success();
    }

    @Override
    public ResultModel groupDown(@RequestBody GoodsGroupInfoDto request) {
        goodsGroupService.down(request.getId());
        return ResultModel.success();
    }

    @Override
    public ResultModel groupRemove(@RequestBody GoodsGroupInfoDto request) {
        goodsGroupService.remove(request.getId());
        return ResultModel.success();
    }

    @Override
    public ResultModel<GoodsGroupInfoVo> groupInfo(@RequestBody GoodsGroupInfoDto request) {
        GoodsGroupBuying goodsGroupBuying = goodsGroupService.get(request.getId());
        GoodsGroupInfoVo vo = GoodsGroupInfoVo.builder()
                .id(goodsGroupBuying.getId())
                .title(goodsGroupBuying.getTitle())
                .photo(goodsGroupBuying.getPhoto())
                .hidePhotos(goodsGroupBuying.getHidePhotos())
                .videoUrl(goodsGroupBuying.getVideoUrl())
                .goodsId(goodsGroupBuying.getGoodsId())
                .startTime(goodsGroupBuying.getStartTime())
                .endTime(goodsGroupBuying.getEndTime())
                .status(goodsGroupBuying.getStatus())
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<GoodsGroupListVo> groupList(@RequestBody GoodsGroupListDto request) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<GoodsGroupBuying> list = CompanyBaseService.isPlatform(request.getCompanyId())//后台请求管理列表
                ? goodsGroupService.getList() : goodsGroupService.getList(request.getCompanyId());
        PageResponse2<GoodsGroupListVo.Group> page = ConvertUtils.page2(list, goodsGroupBuying -> {
            return GoodsGroupListVo.Group.builder()
                    .id(goodsGroupBuying.getId())
                    .title(goodsGroupBuying.getTitle())
                    .photo(goodsGroupBuying.getPhoto())
                    .goodsId(goodsGroupBuying.getGoodsId())
                    .startTime(goodsGroupBuying.getStartTime())
                    .endTime(goodsGroupBuying.getEndTime())
                    .status(goodsGroupBuying.getStatus())
                    .build();
        });

        GoodsGroupListVo vo = GoodsGroupListVo.builder()
                .page(page)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel priceHide(@RequestBody GoodsPriceHideRequest request) {
        goodsService.priceHide(request.getCompanyId());
        return ResultModel.success();
    }

    @Override
    public ResultModel priceShow(@RequestBody GoodsPriceHideRequest request) {
        goodsService.priceShow(request.getCompanyId());
        return ResultModel.success();
    }

    @Override
    public ResultModel priceRemark(@RequestBody GoodsPriceRemarkRequest request) {
        goodsService.priceRemark(request.getGoodsId(), request.getShowPrice(), request.getIsPayment(), request.getSpecs());
        return ResultModel.success();
    }

    @Override
    public ResultModel mainPush(@RequestBody GoodsMainPushRequest request) {
        goodsService.mainPush(request.getGoodsId(), request.isMainPush());
        return ResultModel.success();
    }

    @Override
    public ResultModel show(@RequestBody GoodsShowRequest request) {
        goodsService.show(request.getId());
        return ResultModel.success();
    }

    @Override
    public ResultModel hide(@RequestBody GoodsHideRequest request) {
        goodsService.hide(request.getId());
        return ResultModel.success();
    }

    @Override
    public ResultModel vrUpdate(@RequestBody GoodsVrUpdateRequest request) {
        goodsService.updateQjVrUrl(request.getGoodsId(), request.getQjVrUrl());
        return ResultModel.success();
    }

    @Override
    public ResultModel modelUpdate(@RequestBody GoodsModelUpdateRequest request) {
        goodsService.updateModel(request.getSpecId(), request.getModelPicUrl(), request.getModelUrl(), request.getModelZipPath());
        return ResultModel.success();
    }
}
